package com.example.pma.controllers;

import com.example.pma.dao.CompanyRepository;
import com.example.pma.entities.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("companies")
public class CompanyController {

    @Autowired
    CompanyRepository companyRepository;

    @GetMapping
    public String displayCompanies(Model model){

        List<Company> companies= companyRepository.findAll();
        model.addAttribute("companies",companies);
        return "companies/list-companies";
    }

    @GetMapping("/new")
    public String displayCompanyForm(Model model){
        Company aCompany=new Company();
        model.addAttribute("company",aCompany);
        return "companies/new-company";
    }

    @PostMapping("/save")
    public String createCompany(Model model,Company company){

        companyRepository.save(company);
        return "redirect:/companies";

    }
}
